package com.jxwifi.cloud.quickcleanserver.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickcleanserver.R;
import com.jxwifi.cloud.quickcleanserver.app.StateUtils;
import com.jxwifi.cloud.quickcleanserver.bean.OrderFromBean;
import com.jxwifi.cloud.quickcleanserver.order.OrderDetailsActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/5/15.
 */

public class MyOrderListAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<OrderFromBean> mList = new ArrayList<>();

    public MyOrderListAdapter(Context ctx) {
        mContext = ctx;
    }

    /**
     * 下拉刷新时调用
     *
     * @param list
     */
    public void refresh(List<OrderFromBean> list) {
        mList.clear();
        addData(list);
    }

    public void updata() {
        mList.clear();
        notifyDataSetChanged();
    }

    //添加数据
    public void addData(List<OrderFromBean> list) {
        if (list.size() != 0 || list != null) {
            mList.addAll(list);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_my_order_item, null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ViewHolder mViewHolder = (ViewHolder) holder;
        final OrderFromBean mAskBean = mList.get(position);

        if (StateUtils.SER_END.equals(mAskBean.getOrderStatus())){
            mViewHolder.mTvMyOrderItemState.setText("已结算");
        }else{
            mViewHolder.mTvMyOrderItemState.setText("未结算");
        }
        mViewHolder.mTvMyOrderItemPrice.setText(mAskBean.getRealAmount()+"元");
        mViewHolder.mTvMyOrderItemTimeText.setText(mAskBean.getServiceTime());
        mViewHolder.mTvMyOrderItemAddressText.setText(mAskBean.getAddress());

        mViewHolder.mRelMyOrderItem.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                Intent intent=new Intent();
                intent.setClass(mContext, OrderDetailsActivity.class);
                intent.putExtra("order_code",mAskBean.getOrderCode());
                mContext.startActivity(intent);
            }
        });


    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View mContainer;
        TextView mTvMyOrderItemState;//状态
        TextView mTvMyOrderItemPrice;//价格
        TextView mTvMyOrderItemTimeText;//时间
        TextView mTvMyOrderItemAddressText;//地址
        RelativeLayout mRelMyOrderItem;


        public ViewHolder(View itemView) {
            super(itemView);
            mContainer = itemView;
            mTvMyOrderItemState = (TextView) itemView.findViewById(R.id.tv_my_order_item_state);
            mTvMyOrderItemPrice = (TextView) itemView.findViewById(R.id.tv_my_order_item_price);
            mTvMyOrderItemTimeText = (TextView) itemView.findViewById(R.id.tv_my_order_item_time_text);
            mTvMyOrderItemAddressText = (TextView) itemView.findViewById(R.id.tv_my_order_item_address_text);
            mRelMyOrderItem=(RelativeLayout)itemView.findViewById(R.id.rel_my_order_item);
        }
    }
}
